/*
 * t_event_serialization.cpp
 *
 *  Created on: 06.05.2011
 *      Author: inkooboo
 */

#include "core/event.hpp"

#include <boost/uuid/uuid_generators.hpp>
#include <boost/timer.hpp>

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char **argv) {

  try {
    cout << "--------- serialization/deserialization correctness test -------------" << endl;

    event_t evt1(evt_invalid, boost::uuids::random_generator()());

    evt1.params().put("test1", "1");
    evt1.params().put("test1.test2", "2");

    std::string serialized_evt = evt1.serialize();

    cout << "serialized string: " << endl << serialized_evt << endl;

    std::istringstream stream(serialized_evt);

    event_t evt2(stream);

    bool equal =
    (evt1.params().get<std::string>("test1") == evt2.params().get<std::string>("test1"))
    &&
    (evt1.params().get<std::string>("test1.test2") == evt2.params().get<std::string>("test1.test2"))
    &&
    (evt1.type() == evt2.type())
    &&
    (evt1.who_is() == evt2.who_is());

    assert(evt2.params().get<std::string>("test1") == "1");
    assert(evt2.params().get<std::string>("test1.test2") == "2");

    cout << "serialized and deserialized events are" << (equal ? " " : " NOT ") << "equal" << endl;

    cout << "--------- serialization speed test -------------" << endl;

    const static int N1 = 10000;

    std::string tmp;
    tmp.reserve(1024);

    boost::timer timer;

    for (size_t i = 0; i < N1; ++i)
    {
        tmp = evt1.serialize();
    }

    cout << N1 << " events serialized over " << timer.elapsed() << " sec" << endl;

    cout << "--------- deserialization speed test -------------" << endl;

    const static int N2 = 10000;

    timer.restart();
    for (size_t i = 0; i < N2; ++i)
    {
        event_t evt3(stream);
        evt3.type();
    }

    cout << N2 << " events deserialized over " << timer.elapsed() << " sec" << endl;

    cout << "---end test---" << endl;
  }
  catch(const exception& e){
    cout<<"Exception: "<< e.what() << endl;
  }

  cin.get();
  return 0;
}

